<?php
/**
 * Created by PhpStorm.
 * User: User
 * Date: 2019/10/31
 * Time: 16:26
 */
namespace app\yq4382s\controller;

use think\Controller;
use think\Db;
use app\common;

class Frontend extends Base {

    /**
     * 首页
     */
    public function index()
    {
        $map["isdie"] = 0;
        $data = Db::name('banner')->where($map)->order('create_at asc')->select();
        $this->assign('data',$data);
        return $this->fetch();
     }

     public function add() {
         if(input('post.')){
            $postdata = input('post.');
             $path =  '/public/uploads/';

             $file = request()->file('img');
             if(!isset($file)){
                 $this->error("请上传图片");
                 return $this->fetch();
             }
             $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
             if($info){
                 $data['filepath'] = $path.$info->getSaveName();
                 $data['title'] = $postdata["title"];
                 $data['text'] = $postdata["textcontent"];
                 $data['create_at'] = time();
                 $ids = Db::name('banner')->insert($data);
                 if ($ids) {
                     $this->redirect('frontend/index');
                 }
             }else{
                 echo $file->getError();
                 return $this->fetch();
             }


         } else {
             return $this->fetch();
         }

     }

     /*
      * 幻灯片删除
      * */
    public function delslide()
    {

        $id = input('get.id',0);
        if(!$id){
            return WPreturn('参数错误',-1);
        }

        $del = Db::name('banner')->where('id', $id)->update(['isdie' => 1]);
        if($del){
            return WPreturn('删除成功',1);
        }else{
            return WPreturn('删除失败',-1);
        }

    }



}